package cn.edu.neusoft.wuguangsheng.market.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.market.R;
import cn.edu.neusoft.wuguangsheng.market.activity.SearchinfoActivity;
import cn.edu.neusoft.wuguangsheng.market.adapter.AllMarketAdapter;
import cn.edu.neusoft.wuguangsheng.market.bean.MarketBean;
import cn.edu.neusoft.wuguangsheng.market.listener.ListListener;
import cn.edu.neusoft.wuguangsheng.market.model.MarketModel;

/**
 * A simple {@link Fragment} subclass.
 */

public class ShopFragment extends BaseFragment {
    ImageView search_btn;
    RecyclerView recyclerView;
    private List<MarketBean> items;
    String search;
    ImageView search_et;
    Boolean up=false;
    LinearLayout layout_dingwei;
    TextView dingwei;
    TextView flagcart;
    LinearLayout lyss;
    ListListener<MarketBean> listListener=new ListListener<MarketBean>() {
        @Override
        public void onResponse(List<MarketBean> list) {
            items=list;
            AllMarketAdapter allMarketAdapter=new AllMarketAdapter(getActivity(),items,R.layout.itemallmarket);
            recyclerView.setAdapter(allMarketAdapter);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
        }

        @Override
        public void onFail(String msg) {

        }
    };
    public ShopFragment() {
        MarketModel marketModel=new MarketModel();
        marketModel.getMarketMessage(listListener);
      }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setLayout_file(R.layout.fragment_shop);
        return super.onCreateView(inflater,container,savedInstanceState);
    }


    @Override
    void initView() {

         LocationClient mLocationClient = null;
         MyLocationListener myListener = new MyLocationListener();

        recyclerView=(RecyclerView) view.findViewById(R.id.recyclerview);
        layout_dingwei=(LinearLayout) view.findViewById(R.id.layout_dingwei);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        search_btn=(ImageView) view.findViewById(R.id.search_btn);
        search_et=(ImageView) view.findViewById(R.id.search_et);
        dingwei=(TextView) view.findViewById(R.id.dingwei);
        lyss=(LinearLayout) view.findViewById(R.id.lyss);
        mLocationClient = new LocationClient(getContext());
        //声明LocationClient类
        mLocationClient.registerLocationListener(myListener);
        //注册监听函数
        LocationClientOption option = new LocationClientOption();
        option.setIsNeedAddress(true);
//可选，是否需要地址信息，默认为不需要，即参数为false
//如果开发者需要获得当前点的地址信息，此处必须为true

        mLocationClient.setLocOption(option);
//mLocationClient为第二步初始化过的LocationClient对象
//需将配置好的LocationClientOption对象，通过setLocOption方法传递给LocationClient对象使用
//更多LocationClientOption的配置，请参照类参考中LocationClientOption类的详细说明
        mLocationClient.start();

//mLocationClient为第二步初始化过的LocationClient对象
//调用LocationClient的start()方法，便可发起定位请求

//
//        layout_dingwei.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//               Intent intentdd=new Intent(getContext(), DituActivity.class);
//               startActivity(intentdd);
//            }
//        });


    }

    @Override
    void initEvent() {
    lyss.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intents=new Intent(getContext(), SearchinfoActivity.class);
        startActivity(intents);
    }
});



    }

    @Override
    void initDate() {

    }

    @Override
    public int getLayout_file() {
        return super.getLayout_file();
    }

    @Override
    public void setLayout_file(int layout_file) {
        super.setLayout_file(layout_file);
    }

    @Override
    public void onPause() {
        super.onPause();
        up=true;

    }

    @Override
    public void onResume() {
        super.onResume();
        if(up){
            initView();
            up=false;
        }
    }


    public class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location){
            //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
            //以下只列举部分获取地址相关的结果信息
            //更多结果信息获取说明，请参照类参考中BDLocation类中的说明
            String addr = location.getAddrStr();    //获取详细地址信息
            String country = location.getCountry();    //获取国家
            String province = location.getProvince();    //获取省份
            String city = location.getCity();    //获取城市
            String district = location.getDistrict();    //获取区县
            String street = location.getStreet();    //获取街道信息
            dingwei.setText(addr);


        }
    }

}
